﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using D3DSS.Http;
using D3DSS.DataContract;
using System.Web.Security;
using System.Configuration;
using D3DSS.FormUI.Properties;

namespace D3DSS.FormUI
{
    public partial class frmRegisterAndLogin : Form
    {
        public frmRegisterAndLogin()
        {
            InitializeComponent();
        }

        private void frmRegisterAndLogin_Load(object sender, EventArgs e)
        {
            chkIsRememberPassword.Checked = Settings.Default.IsRememberPassword;
            if (chkIsRememberPassword.Checked)
            {
                txtAccount.Text = Settings.Default.Account;
                txtPassword.Text = Settings.Default.Password;
            }
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtAccount.Text) && !string.IsNullOrWhiteSpace(txtPassword.Text))
            {
                if (!txtAccount.Text.Contains(".") || !txtAccount.Text.Contains("@"))
                {
                    MessageBox.Show("请填写正确的电子邮件！");
                    return;
                }
                var request = new RegisterRequest();

                request.Account = txtAccount.Text.Trim().ToLower();
                request.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(), "MD5");

                var response = HttpRequest.Post<Response<RegisterResponse>>(Settings.Default.RegisterUrl, request);
                if (response.Item1.IsSuccess)
                {
                    Program.Token = response.Item1.Data.Token;
                    Settings.Default.IsRememberPassword = chkIsRememberPassword.Checked;
                    if (chkIsRememberPassword.Checked)
                    {
                        Settings.Default.Account = txtAccount.Text.Trim().ToLower();
                        Settings.Default.Password = txtPassword.Text.Trim();
                    }
                    Settings.Default.Save();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MessageBox.Show(response.Item1.MessageDictionary.Single(m => m.MessageCode == response.Item1.Code).MessageContent);
                }
            }
            else
            {
                MessageBox.Show("邮箱和密码不能为空！");
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtAccount.Text) && !string.IsNullOrWhiteSpace(txtPassword.Text))
            {
                var request = new LoginRequest();

                request.Account = txtAccount.Text.Trim();
                request.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(), "MD5");

                var response = HttpRequest.Post<Response<LoginResponse>>(Settings.Default.LoginUrl, request);
                if (response.Item1.IsSuccess)
                {
                    Program.Token = response.Item1.Data.Token;
                    Settings.Default.IsRememberPassword = chkIsRememberPassword.Checked;
                    if (chkIsRememberPassword.Checked)
                    {
                        Settings.Default.Account = txtAccount.Text.Trim().ToLower();
                        Settings.Default.Password = txtPassword.Text.Trim();
                    }
                    Settings.Default.Save();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MessageBox.Show(response.Item1.MessageDictionary.Single(m => m.MessageCode == response.Item1.Code).MessageContent);
                }
            }
            else
            {
                MessageBox.Show("邮箱和密码不能为空！");
            }
        }

    }
}
